/*
 * @(#)RestartService.java   1.0  2021年11月8日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.service;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swift.core.auth.Login;
import com.swift.core.env.ConfigInterface;
import com.swift.core.model.ServiceRequest;
import com.swift.core.model.data.DataModel;
import com.swift.core.service.SynInterface;
import com.swift.core.validator.DataValidator;
import com.swift.core.validator.annotation.ParamNotNull;
import com.swift.dts.loadconfig.FileSysConfig;
import com.swift.dts.loadconfig.model.DtsDataBase;
import com.swift.dts.loadconfig.model.SysConfig;
import com.swift.dts.loadconfig.model.TablesConfig;

/**
 * 启动或者重启服务使用
 * 
 * @author zhengjiajin
 * @version 1.0 2021年11月8日
 */
@Service("reloadService")
@Login
public class ReloadService implements SynInterface {

    @Autowired
    private ConfigInterface<DtsDataBase> dataBaseConfigInterface;
    @Autowired
    private ConfigInterface<TablesConfig> tablesConfigInterface;
    @Autowired
    private FileSysConfig fileSysConfig;
    @PostConstruct
    public void init() {
        SysConfig sysConfig = fileSysConfig.read();
        if(sysConfig.getStartConfig()) {
            restart();
        }
    }
    
    private void restart() {
        new ReloadServiceThread().start();
    }
    
    private class ReloadServiceThread extends Thread {
        
        public void run() {
            dataBaseConfigInterface.refresh();
            tablesConfigInterface.refresh();
        }
    }

    /**
     * @see com.swift.core.service.SynInterface#doService(com.swift.core.model.ServiceRequest)
     */
    @Override
    @DataValidator(NotNull = { @ParamNotNull(param = "reload", message = "reload参数不能为空") })
    public DataModel doService(ServiceRequest req) {
        boolean reload = req.getData().getBoolean("reload");
        if (!reload) return null;
        restart();
        return null;
    }

}
